﻿Public Class ReportPrintForm
    Public Property RptPrintingSystem As New DevExpress.XtraPrinting.PrintingSystemBase
    Public Property Report As DevExpress.XtraReports.UI.XtraReport
    Public Property CertificateOfAnalysisId As Guid
    Public Property BinaryString() As Byte()

    Private Sub ReportPrintForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Report IsNot Nothing Then
            MyPrintCtrl.PrintingSystem = Report.PrintingSystem
        Else
            MyPrintCtrl.PrintingSystem = RptPrintingSystem
        End If

    End Sub

    Private Sub PrintPreviewBarItem7_ItemClick(sender As Object, e As ItemClickEventArgs) Handles PrintPreviewBarItem7.ItemClick

        Dim result As DialogResult = Windows.Forms.DialogResult.OK
        If BinaryString IsNot Nothing AndAlso BinaryString.Length > 0 Then
            result = MessageWindow.ShowMessage("Save will overwrite current print.")
        End If

        If result = Windows.Forms.DialogResult.OK Then
            SavePrintInDB()
        End If

    End Sub

    Public Function SavePrintInDB() As Boolean
        Dim sqlcmd As SqlCommand = Nothing

        Using reportStream As IO.MemoryStream = New IO.MemoryStream()
            Try
                Report.ExportToPdf(reportStream)

                Using streamReader As IO.StreamReader = New IO.StreamReader(reportStream)

                    sqlcmd = MetaQuery.GetSPCmdByMetaName("CertificateOfAnalysis_SavePrint")
                    sqlcmd.Parameters("@CertificateOfAnalysisId").Value = CertificateOfAnalysisId
                    sqlcmd.Parameters("@BinaryData").Value = reportStream.ToArray

                    sqlcmd.ExecuteNonQuery()
                End Using
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Using

        Return True
    End Function

    Public Sub PreviewPrintedCOA()

        Dim reportString As String
        Dim data() As Byte
        Dim encoder As System.Text.UTF8Encoding
        Dim filePath As String = DeskTopManager.ApplicationDataFolder + "\" + CertificateOfAnalysisId.ToString + ".tsf"
        Using ms As IO.MemoryStream = New IO.MemoryStream()
            encoder = New System.Text.UTF8Encoding()
            '
            reportString = System.Text.Encoding.UTF8.GetString(BinaryString)
            data = encoder.GetBytes(reportString)
            ms.Write(data, 0, data.Length)

            Dim fs As IO.FileStream = New IO.FileStream(filePath, IO.FileMode.Create)
            fs.Write(BinaryString, 0, BinaryString.Length)
            fs.Close()
            '
            Dim frm As PDFReaderForm = New PDFReaderForm()
            frm.FilePath = filePath
            frm.Show()
            
        End Using

    End Sub
End Class